package component;

import com.fasterxml.jackson.core.JsonProcessingException;
import dao.EmployerDao;
import listener.ActionDoneListener;
import model.Employer;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.Book;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class EmployerManageComponent extends Box {
    final int WIDTH=850;
    final int HEIGHT=600;
    private EmployerDao employerDao = new EmployerDao();

    JFrame jf = null;
    private JTable table;
    private Vector<String> titles;
    private Vector<Vector> tableData;
    private DefaultTableModel tableModel;



    public EmployerManageComponent(JFrame jf) throws JsonProcessingException, SQLException {
        //垂直布局
        super(BoxLayout.Y_AXIS);
        //组装视图
        this.jf = jf;
        JPanel btnPanel = new JPanel();
        Color color = new Color(203,220,217);
        btnPanel.setBackground(color);
        btnPanel.setMaximumSize(new Dimension(WIDTH,80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton addBtn = new JButton("添加");
        JButton updateBtn = new JButton("修改");
        JButton deleteBtn = new JButton("删除");
        JButton degree_btn = new JButton("学历查询");
        JButton marriage_btn = new JButton("婚姻查询");
        JButton dept_btn = new JButton("部门查询");
        JButton all_btn = new JButton("整体查询");

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹出一个对话框，让用户输入图书的信息
                new AddBEmployerDialog(jf, "新增员工", true, new ActionDoneListener() {
                    @Override
                    public void done(Object result) throws JsonProcessingException, SQLException {
                        tableData.clear();
                        List<Employer>  employers = employerDao.findAll();
                        requestData(employers);
                    }
                }).setVisible(true);
            }
        });

        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取当前表格选中的id
                int selectedRow = table.getSelectedRow();//如果有选中的条目，则返回条目的行号，如果没有选中，那么返回-1

                if (selectedRow==-1){
                    JOptionPane.showMessageDialog(jf,"请选择要修改的条目！");
                    return;
                }

                String id = tableModel.getValueAt(selectedRow, 0).toString();

                //弹出一个对话框，让用户修改
                try {
                    new UpdateEmployerDialog(jf, "修改员工信息", true, new ActionDoneListener() {
                        @Override
                        public void done(Object result) throws JsonProcessingException, SQLException {
                            tableData.clear();
                            List<Employer>  employers = employerDao.findAll();
                            requestData(employers);
                        }
                    },id).setVisible(true);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });

        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取选中的条目
                int selectedRow = table.getSelectedRow();//如果有选中的条目，则返回条目的行号，如果没有选中，那么返回-1

                if (selectedRow==-1){
                    JOptionPane.showMessageDialog(jf,"请选择要删除的条目！");
                    return;
                }

                //防止误操作
                int result = JOptionPane.showConfirmDialog(jf, "确认要删除选中的条目吗？", "确认删除", JOptionPane.YES_NO_OPTION);
                if (result != JOptionPane.YES_OPTION){
                    return;
                }

                String id = tableModel.getValueAt(selectedRow, 0).toString();
                boolean flag = false;
                try {
                    flag = employerDao.deleteByid(Integer.parseInt(id));
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                if (flag){
                    //删除成功
                    JOptionPane.showMessageDialog(jf,"删除成功");
                    try {
                        tableData.clear();
                        List<Employer>  employers = employerDao.findAll();
                        requestData(employers);
                    } catch (JsonProcessingException ex) {
                        ex.printStackTrace();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }else{
                    //删除失败
                    JOptionPane.showMessageDialog(jf,"删除失败");
                }

            }
        });

        degree_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹出一个对话框，让用户输入员工学历
                new FindByDegreeDialog(jf, "输入员工学历", true, new ActionDoneListener() {
                    @Override
                    public void done(Object result) throws JsonProcessingException, SQLException {
                        List<Employer>  employers = employerDao.findByDegree(FindByDegreeDialog.DEGREE);
                        System.out.println(employers);
                        tableData.clear();
                        requestData(employers);
                    }
                }).setVisible(true);
            }
        });

        marriage_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹出一个对话框，让用户输入员工学历
                new FindByMarriageDialog(jf, "输入婚姻状况", true, new ActionDoneListener() {
                    @Override
                    public void done(Object result) throws JsonProcessingException, SQLException {
                        List<Employer>  employers = employerDao.findByMarriage(FindByMarriageDialog.MARRIAGE);
                        System.out.println(employers);
                        tableData.clear();
                        requestData(employers);
                    }
                }).setVisible(true);
            }
        });

        dept_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹出一个对话框，让用户输入员工学历
                new FindByDeptDialog(jf, "选择查询部门", true, new ActionDoneListener() {
                    @Override
                    public void done(Object result) throws JsonProcessingException, SQLException {
                        List<Employer>  employers = employerDao.findByDept(FindByDeptDialog.DEPT);
                        System.out.println(employers);
                        tableData.clear();
                        requestData(employers);
                    }
                }).setVisible(true);
            }
        });


        all_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Employer>  employers = null;
                try {
                    employers = employerDao.findAll();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                tableData.clear();
                try {
                    requestData(employers);
                } catch (JsonProcessingException ex) {
                    ex.printStackTrace();
                }
            }
        });

        btnPanel.add(addBtn);
        btnPanel.add(updateBtn);
        btnPanel.add(deleteBtn);
        btnPanel.add(degree_btn);
        btnPanel.add(dept_btn);
        btnPanel.add(marriage_btn);
        btnPanel.add(all_btn);
        this.add(btnPanel);

        //组装表格
        String[] ts = {"编号","姓名","年龄","性别","部门" , "婚姻","学历" , "职称"};
        titles = new Vector<>();
        for (String title : ts) {
            titles.add(title);
        }

        tableData = new Vector<>();

        tableModel = new DefaultTableModel(tableData,titles);
        table = new JTable(tableModel){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        //设置只能选中一行
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
        List<Employer>  employers = employerDao.findAll();
        System.out.println(employers);
        requestData(employers);
    }

    //请求数据
    public void requestData(List<Employer> employers) throws JsonProcessingException {

        String[] ts = new String[8];
        for (Employer employer : employers) {

            ts[0] = Integer.toString(employer.getId());
            ts[1] = employer.getName();
            ts[2] = employer.getAge();
            ts[3] = employer.getSex();
            ts[4] = employer.getDept();
            ts[5] = employer.getMarriage();
            ts[6] = employer.getDegree();
            ts[7] = employer.getTitle();
            Vector titless = new Vector<>();
            for (String title : ts) {
                titless.add(title);
            }
            tableData.add(titless);
            tableModel.fireTableDataChanged();
        }


    }



}
